package com.thssh.ui.lsn06.view;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.EmbossMaskFilter;
import android.graphics.LightingColorFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/19
 * email: zhangyugehu@163.com
 */

public class FilterView extends View {

    private int mWidth;
    private int mHeight;

    private Paint mPaint;

    private int mTypeCounter;
    private int mTypeCount = 9;
    private MaskFilter mMaskFilterType;
    private ColorFilter mColorFilterType;
    private int mBlurRadius = 50;
    private String mBlurName = "Blur.NORMAL";

    public FilterView(Context context) {
        this(context, null);
    }

    public FilterView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FilterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mMaskFilterType = new BlurMaskFilter(mBlurRadius, BlurMaskFilter.Blur.NORMAL);
        initPaint();
    }


    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private boolean isDown = false;
    float startX = 0, startY = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startX = (int) event.getX();
                startY = (int) event.getY();
                isDown = true;
                break;
            case MotionEvent.ACTION_MOVE:
                if(Math.abs(event.getX() - startX) > 50 || Math.abs(event.getY() - startY) > 50) {
                    isDown = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                if(!isDown){
                    break;
                }
                if(++mTypeCounter > mTypeCount){
                    mTypeCounter = 0;
                }
                switch (mTypeCounter){
                    case 0:
                        mMaskFilterType = new BlurMaskFilter(mBlurRadius, BlurMaskFilter.Blur.NORMAL);
                        mBlurName = "Blur.NORMAL";
                        break;
                    case 1:
                        mMaskFilterType = new BlurMaskFilter(mBlurRadius, BlurMaskFilter.Blur.INNER);
                        mBlurName = "Blur.INNER";
                        break;
                    case 2:
                        mMaskFilterType = new BlurMaskFilter(mBlurRadius, BlurMaskFilter.Blur.OUTER);
                        mBlurName = "Blur.OUTER";
                        break;
                    case 3:
                        mMaskFilterType = new BlurMaskFilter(mBlurRadius, BlurMaskFilter.Blur.SOLID);
                        mBlurName = "Blur.SOLID";
                        break;
                    case 4:
                        /**
                         * Create an emboss maskfilter
                         * 浮雕效果
                         * @param direction
                         *      光源位置
                 *              array of 3 scalars [x, y, z] specifying the direction of the light source
                         * @param ambient
                         *      环境光因子,越趋近0，越暗
                         *      0...1 amount of ambient light
                         * @param specular
                         *      镜面反射系数，越趋近0，越强
                         *      coefficient for specular highlights (e.g. 8)
                         * @param blurRadius
                         *       模糊半径
                         *      amount to blur before applying lighting (e.g. 3)
                         * @return           the emboss maskfilter
                         */
                        mMaskFilterType = new EmbossMaskFilter(
                                new float[]{1, 0, 0},
                                0.2f,
                                60,
                                80);
                        mBlurName = "EmbossMaskFilter[1, 0, 0]";
                        break;
                    case 5:
                        mMaskFilterType = new EmbossMaskFilter(
                                new float[]{1, 1, 0},
                                0.9f,
                                60,
                                80);
                        mBlurName = "EmbossMaskFilter[1, 1, 0]";
                        break;
                    case 6:
                        mMaskFilterType = new EmbossMaskFilter(
                                new float[]{0, 0, 1},
                                0.9f,
                                60,
                                80);
                        mBlurName = "EmbossMaskFilter[0, 0, 1]";
                        break;
                    case 7:
                        mMaskFilterType = null;
                        mColorFilterType = new ColorMatrixColorFilter((new ColorMatrix(new float[]{
                                1,  0,  0,  0,      0,// *R,  0,  0,  0, +ExtR,
                                0,  1,  0,  0,      0,//  0,  G,  0,  0, +ExtG,
                                0,  0,  1,  0,      0,//  0, *0, *B,  0, +ExtB,
                                0,  0,  0,  0.5f,   0 //  0,  0,  0, *A, +ExtA
                        })));
                        mBlurName = "ColorMatrixColorFilter";
                        break;
                    case 8:
                        mMaskFilterType = null;
                        mColorFilterType = new LightingColorFilter(0xffffff, 100);
                        mBlurName = "LightingColorFilter";
                        break;
                    case 9:
                        mMaskFilterType = null;
                        mColorFilterType = new PorterDuffColorFilter(Color.argb(255, 149, 90, 200), PorterDuff.Mode.DST_IN);
                        mBlurName = "PorterDuffColorFilter";
                        break;

                }
                invalidate();
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rectF = new RectF(100, 100, 300, 300);
        mPaint.setColor(Color.RED);
        if(mMaskFilterType == null){
            mPaint.setColorFilter(mColorFilterType);
        }else {
            mPaint.setMaskFilter(mMaskFilterType);
        }
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(rectF, mPaint);

        mPaint.setMaskFilter(null);
        mPaint.setColorFilter(null);

        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setTextSize(30);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLACK);
        canvas.drawText(mBlurName, (rectF.left + rectF.right)/2, rectF.top, mPaint);
    }
}
